package components

import (
	"image/color"
	"pass-tree/pkg/utils"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/data/binding"
	"fyne.io/fyne/v2/driver/desktop"
	"fyne.io/fyne/v2/widget"
)

// CustomButton 自定义按钮组件
type CustomButton struct {
	widget.BaseWidget
	text       string
	icon       fyne.Resource
	onClick    func()
	isLoading  binding.Bool
	isHovered  bool
	bgColor    color.Color
	hoverColor color.Color
	textColor  color.Color

	// 组件元素
	background *canvas.Rectangle
	label      *canvas.Text
	iconObj    *canvas.Image
	spinner    *widget.ProgressBarInfinite
	container  *fyne.Container
}

// NewButton 创建自定义按钮
// func NewButton(text string, icon fyne.Resource, onClick func()) *CustomButton {
func NewButton(text string, onClick func()) *CustomButton {
	cb := &CustomButton{
		text: text,
		// icon:       icon,
		onClick:   onClick,
		isLoading: binding.NewBool(),
		// bgColor:    color.NRGBA{R: 253, G: 253, B: 0, A: 253},       // 蓝色
		bgColor:    utils.HexToNRGBA("#FDFDFD"), // 蓝色
		hoverColor: utils.HexToNRGBA("#B7DEFF"), // 深蓝色
		textColor:  utils.HexToNRGBA("#565656"),
	}
	cb.ExtendBaseWidget(cb)
	cb.createComponents()
	return cb
}

// 创建组件元素
func (c *CustomButton) createComponents() {
	c.background = canvas.NewRectangle(c.bgColor)
	c.background.CornerRadius = 3                          // 设置3像素圆角
	c.background.StrokeColor = utils.HexToNRGBA("#0078D4") // 边框颜色
	c.background.StrokeWidth = 1                           // 边框宽度(像素)
	// 设置固定高度和宽度
	c.background.SetMinSize(fyne.NewSize(10, 10)) // 宽度0表示自动，高度100
	c.background.Resize(fyne.NewSize(10, 10))     // 宽度0表示自动，高度100

	c.label = canvas.NewText(c.text, c.textColor)
	c.label.Alignment = fyne.TextAlignCenter

	/* if c.icon != nil {
		c.iconObj = canvas.NewImageFromResource(c.icon)
		c.iconObj.FillMode = canvas.ImageFillContain
	} */

	c.spinner = widget.NewProgressBarInfinite()
	c.spinner.Hide()

	content := container.NewHBox()
	/* if c.iconObj != nil {
		content.Add(c.iconObj)
	} */
	content.Add(c.label)

	c.container = container.NewStack(c.background, content)

	/* // 使用VBox或HBox布局，但限制高度
	c.container = container.NewVBox(
		container.NewStack(
			c.background,
			layout.NewSpacer(), // 确保高度被遵守
		),
	)
	c.container.MinSize().Height = 10 // 再次确认 */
	/* // 监听加载状态
	c.isLoading.AddListener(binding.NewDataListener(func() {
		loading, _ := c.isLoading.Get()
		if loading {
			c.label.Hide()
			if c.iconObj != nil {
				c.iconObj.Hide()
			}
			c.spinner.Show()
			c.background.FillColor = color.NRGBA{R: 0x99, G: 0x99, B: 0x99, A: 0xFF} // 灰色
		} else {
			c.label.Show()
			if c.iconObj != nil {
				c.iconObj.Show()
			}
			c.spinner.Hide()
			c.background.FillColor = c.bgColor
		}
		c.background.Refresh()
	})) */
}

// SetText 设置按钮文本
func (c *CustomButton) SetText(text string) {
	c.text = text
	c.label.Text = text
	c.Refresh()
}

// SetIcon 设置按钮图标
func (c *CustomButton) SetIcon(icon fyne.Resource) {
	c.icon = icon
	if c.iconObj != nil {
		c.container.Remove(c.iconObj)
	}

	if icon != nil {
		c.iconObj = canvas.NewImageFromResource(icon)
		c.iconObj.FillMode = canvas.ImageFillContain
		// 重新构建内容
		content := container.NewHBox(c.iconObj, c.label)
		c.container.Objects = []fyne.CanvasObject{c.background, content, c.spinner}
	}
	c.Refresh()
}

// SetOnClick 设置点击回调
func (c *CustomButton) SetOnClick(onClick func()) {
	c.onClick = onClick
}

// SetLoading 设置加载状态
func (c *CustomButton) SetLoading(loading bool) {
	c.isLoading.Set(loading)
}

// MouseIn 实现桌面接口 - 鼠标进入
func (c *CustomButton) MouseIn(event *desktop.MouseEvent) {
	/* if !c.isLoading.Get() {
	} */
	c.isHovered = true
	c.background.FillColor = c.hoverColor
	c.background.Refresh()
}

// MouseOut 实现桌面接口 - 鼠标离开
func (c *CustomButton) MouseOut() {
	/* if !c.isLoading.Get() {
	} */
	c.isHovered = false
	c.background.FillColor = c.bgColor
	c.background.Refresh()
}

// MouseMoved 实现桌面接口 - 鼠标移动
func (c *CustomButton) MouseMoved(event *desktop.MouseEvent) {}

// Tapped 实现 Widget 接口 - 点击事件
func (c *CustomButton) Tapped(event *fyne.PointEvent) {
	/* if !c.isLoading.Get() && c.onClick != nil {
		c.isLoading.Set(true)
		c.onClick()
		// 恢复状态逻辑需在回调中处理
	} */
}

// CreateRenderer 实现 Widget 接口
func (c *CustomButton) CreateRenderer() fyne.WidgetRenderer {
	return &customButtonRenderer{
		button:    c,
		container: c.container,
	}
}

// customButtonRenderer 按钮渲染器
type customButtonRenderer struct {
	button    *CustomButton
	container *fyne.Container
}

// Layout 实现 Renderer 接口
func (r *customButtonRenderer) Layout(size fyne.Size) {
	r.container.Resize(size)
	r.button.background.Resize(size)

	contentSize := fyne.NewSize(size.Width-20, size.Height-10)
	r.container.Objects[1].Resize(contentSize)
	r.container.Objects[1].Move(fyne.NewPos(10, 5))

	spinnerSize := fyne.NewSize(size.Width-20, 10)
	r.button.spinner.Resize(spinnerSize)
	r.button.spinner.Move(fyne.NewPos(10, (size.Height-10)/2))
}

// MinSize 实现 Renderer 接口
func (r *customButtonRenderer) MinSize() fyne.Size {
	textSize := r.button.label.MinSize()
	iconSize := fyne.NewSize(0, 0)
	if r.button.iconObj != nil {
		iconSize = fyne.NewSize(20, 20)
	}

	return fyne.NewSize(
		textSize.Width+iconSize.Width+30,
		fyne.Max(textSize.Height, iconSize.Height)+20,
	)
}

// Refresh 实现 Renderer 接口
func (r *customButtonRenderer) Refresh() {
	r.container.Refresh()
}

// Objects 实现 Renderer 接口
func (r *customButtonRenderer) Objects() []fyne.CanvasObject {
	return []fyne.CanvasObject{r.container}
}

// Destroy 实现 Renderer 接口
func (r *customButtonRenderer) Destroy() {}
